<?php

class Session {

	public $message;
	public $rights;

	/**
	 * Used to initialise setting and getting the sessions not directly from
	 * session but from an array which is stored in session with $prefix as key.
	 * Ex.: $_SESSION[$prefix][$otherVar]
	 *
	 * @var boolean|string
	 */
	public $prefix = false;

	/**
	 * constructor
	 */
	function __construct($ignorePrefix = false) {
		session_save_path(conf('SESSION_PATH'));
		session_name(conf('SESSION_NAME'));
		session_set_cookie_params(conf('SESSION_COOKIE_LIFETIME'), '/', conf('SESSION_COOKIE_URL'));
		session_start();
		if (!$ignorePrefix) {
			$this->prefix = COUNTRY.'_'.PROJECT;
		}
		$this->checkMessage();
	}

	/**
	 * Set/Get message to session
	 *
	 * @param string $msg
	 * @param boolean $error Is an error message?
	 * @return string
	 */
	public function message($msg="", $error=false) {
		if(!empty($msg)) {
			// then this is "set message"
			if (strpos($this->Get('message'), $msg) === false) {
				$args = func_get_args();
				array_shift($args);
				array_shift($args);
				$this->Set('message', Utils::showMessage($msg, $error, $args), true);
			}
			return true;
		} else {
			// then this is "get message"
			return $this->message;
		}
	}

	/**
	 * Is there a message stored in the session, get message delete session message
	 */
	private function checkMessage() {
		// Is there a message stored in the session?
		$msg = $this->Get('message');
		if(!empty($msg)) {
			// Add it as an attribute and erase the stored version
			$this->message = $msg;
			$this->Remove('message');
		} else {
			$this->message = "";
		}
	}

	/**
	 * Remove session attribute
	 *
	 * @param string $name 
	 */
	public function Remove($name = NULL) {
		if (isset($name)) {
			if ($this->prefix) {
				unset($_SESSION[$this->prefix][$name]);
			} else {
				unset($_SESSION[$name]);
			}
		} else {
			if ($this->prefix) {
				$_SESSION[$this->prefix] = array();
				unset($_SESSION[$this->prefix]);
			} else {
				$_SESSION = array();
				unset($_SESSION);
				session_unset();
				session_destroy();
			}
		}
	}

	/**
	 * set session attribute
	 *
	 * @param string|array $name can only be string or array with exactly 2 elements without keys
	 * @param string|array $value
	 * @param bool $append
	 * @param mixed $appendKey in case the $append is true and the $value is an array, it will concatenate this array to the main array using the $appendKey
	 * @return void
	 */
	public function Set($name, $value, $append = false, $appendKey = NULL) {
		// Whether or not to use the prefix
		if ($this->prefix) {
			// If the name of the session is an array of 2 strings put these strings as [key][sub_key]
			if (is_array($name) && count($name) == 2) {
				// If the $append variable is set tot true append the given $value to the existing one
				if ($append) {
					// If the value is an array we can't concatenate it as usual and we have to concatenate like different arrays: array[] = value
					if (is_array($value)) {
						if ($appendKey == NULL) {
							$_SESSION[$this->prefix][$name[0]][$name[1]][] = $value;
						} else {
							$_SESSION[$this->prefix][$name[0]][$name[1]][$appendKey] = $value;
						}
					} else {
						if (!isset($_SESSION[$this->prefix][$name[0]][$name[1]])) {
							$_SESSION[$this->prefix][$name[0]][$name[1]] = $value;
						} else {
							$_SESSION[$this->prefix][$name[0]][$name[1]] .= $value;
						}
					}
				} else {
					$_SESSION[$this->prefix][$name[0]][$name[1]] = $value;
				}
			} else {
				if ($append) {
					if (is_array($value)) {
						if ($appendKey == NULL) {
							$_SESSION[$this->prefix][$name][] = $value;
						} else {
							$_SESSION[$this->prefix][$name][$appendKey] = $value;
						}
					} else {
						if (!isset($_SESSION[$this->prefix][$name])) {
							$_SESSION[$this->prefix][$name] = $value;
						} else {
							$_SESSION[$this->prefix][$name] .= $value;
						}
					}
				} else {
					$_SESSION[$this->prefix][$name] = $value;
				}
			}
		} else {
			if (is_array($name) && count($name) == 2) {
				if ($append) {
					if (is_array($value)) {
						if ($appendKey == NULL) {
							$_SESSION[$name[0]][$name[1]][] = $value;
						} else {
							$_SESSION[$name[0]][$name[1]][$appendKey] = $value;
						}
					} else {
						if (!isset($_SESSION[$name[0]][$name[1]])) {
							$_SESSION[$name[0]][$name[1]] = $value;
						} else {
							$_SESSION[$name[0]][$name[1]] .= $value;
						}
					}
				} else {
					$_SESSION[$name[0]][$name[1]] = $value;
				}
			} else {
				if ($append) {
					if (is_array($value)) {
						if ($appendKey == NULL) {
							$_SESSION[$name][] = $value;
						} else {
							$_SESSION[$name][$appendKey] = $value;
						}
					} else {
						if (!isset($_SESSION[$name])) {
							$_SESSION[$name] = $value;
						} else {
							$_SESSION[$name] .= $value;
						}
					}
				} else {
					$_SESSION[$name] = $value;
				}
			}
		}
	}

	/**
	 * Check if exist specific session attribute
	 *
	 * @param string $name
	 * @return bool
	 */
	public function Exist($name) {
		if ($this->prefix) {
			return isset($_SESSION[$this->prefix][$name]);
		} else {
			return isset($_SESSION[$name]);
		}
	}

	/**
	 * Get session attribute
	 *
	 * @param string $name
	 * @return mixed
	 */
	public function Get($name = NULL) {
		if (isset($name)) {
			if ($this->Exist($name)) {
				if ($this->prefix) {
					return $_SESSION[$this->prefix][$name];
				} else {
					return $_SESSION[$name];
				}
			} else {
				return false;
			}
		} else {
			if ($this->prefix) {
				return $_SESSION[$this->prefix];
			} else {
				return $_SESSION;
			}
		}
	}

	/**
	 * this function clear all session cookie and is used in logout action
	 */
	public function clearSessionCookie() {
		$params = session_get_cookie_params();
		setcookie(session_name(), '', 0, $params['path'], $params['domain'], $params['secure'], isset($params['httponly']));
	}
}